<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="文件夹名称" prop="folderName">
        <el-input v-model="queryParams.folderName" placeholder="请输入文件夹名称" clearable />
      </el-form-item>
      <!-- <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择项目">
          <el-option v-for="item in projectOptions" :key="item.id" :label="item.projectName" :value="item.id" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="display: flex; flex-wrap: wrap; align-items: center; margin-left: 8px; margin-bottom: 8px;">
      <!-- <div style="display: flex; align-items: center; min-width: 220px; margin-right: 12px;">
        <span style="font-size: 14px; color: #606266; white-space: nowrap;">所属项目:</span>
        <el-select v-model="queryParams.projectId" placeholder="请选择项目"
          style="width: 140px; margin-left: 10px; min-width: 100px; flex-shrink: 0;">
          <el-option v-for="item in projectOptions" :key="item.id" :label="item.projectName" :value="item.id" />
        </el-select>
      </div> -->

      <!-- <div style="display: flex; flex-wrap: wrap; gap: 8px;">
        <el-button v-for="item in dict.type.video_class" :key="item.value" size="mini"
          :type="queryParams.fileType === item.value ? 'primary' : 'default'" @click="handleProcessDoc(item)"
          style="min-width: 80px;">
          {{ item.label }}
        </el-button>
      </div> -->
    </div>

    <!-- <el-table v-loading="loading" :data="projectFolderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="文件夹名称" align="center" prop="folderName" />
      <el-table-column label="文件夹类型" align="center" prop="fileType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['production:projectFolder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['production:projectFolder:remove']">删除</el-button>
        </template>
</el-table-column>
</el-table> -->

    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" /> -->
    <!-- :project-id="queryParams.projectId" -->
    <MacosFileManager2 :folder-name="searchParams.folderName" :file-type="queryParams.fileType" />
    <!-- 添加或修改项目文件夹对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件夹名称" prop="folderName">
          <el-input v-model="form.folderName" placeholder="请输入文件夹名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectFolder2, getProjectFolder2, delProjectFolder2, addProjectFolder2, updateProjectFolder2 } from "@/api/production/companyNoticeFolder";
import MacosFileManager2 from "@/views/components/wordPage/MacosFileManager2";
import { listProjectContractInfo, listAllProjectContractInfo } from "@/api/production/projectContractInfo";

export default {
  name: "ProjectFolder",
  dicts: ["process_documentation", "video_class"],
  components: {
    MacosFileManager2
  },
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      projectOptions: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目文件夹表格数据
      projectFolderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        folderName: null,
        fileType: '97',
        // projectId: null,
      },
      // 新增：用于传递给子组件的已确认搜索参数
      searchParams: {
        folderName: null,
        fileType: null,
        // projectId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        folderName: [
          { required: true, message: "文件夹名称不能为空", trigger: "blur" }
        ],
        fileType: [
          { required: true, message: "文件夹类型不能为空", trigger: "change" }
        ],
      },
    };
  },
  created() {
    // this.fetchProjectOptions(); // 获取项目
    // this.getList();
  },
  methods: {
    // 获取项目列表
    fetchProjectOptions() {
      listAllProjectContractInfo().then(res => {
        // console.log(1, res);
        this.projectOptions = res.data || [];
        // 默认选中第一个项目
        if (this.projectOptions.length > 0 && !this.queryParams.projectId) {
          this.queryParams.projectId = this.projectOptions[0].id;
          this.handleQuery();
        }
      });
    },

    /** 查询项目文件夹列表 */
    getList() {
      this.loading = true;
      listProjectFolder2(this.queryParams).then(response => {
        this.projectFolderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        folderName: null,
        fileType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      // 新增：将当前查询参数赋值给 searchParams
      this.searchParams = {
        folderName: this.queryParams.folderName,
        fileType: this.queryParams.fileType,
        // projectId: this.queryParams.projectId,
      };
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      // 默认选中第一个项目
      // if (this.projectOptions.length > 0) {
      //   this.queryParams.projectId = this.projectOptions[0].id;
      // }
      // 新增：重置文件类型为第一个
      // const arr = this.dict.type.video_class;
      // if (arr && arr.length > 0) {
      //   this.queryParams.fileType = arr[0].value;
      // }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目文件夹";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProjectFolder2(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目文件夹";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProjectFolder2(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectFolder2(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目文件夹编号为"' + ids + '"的数据项？').then(function () {
        return delProjectFolder2(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('production/projectFolder/export', {
        ...this.queryParams
      }, `projectFolder_${new Date().getTime()}.xlsx`)
    },
    // 新增：字典加载完成后，自动选中第一个
    onDictReady(dict) {
      // console.log(1, dict);
      const arr = this.dict.type.video_class;
      if (arr && arr.length > 0) {
        // this.queryParams.fileType = arr[0].value;
        // this.handleProcessDoc(arr[0]);
        // 如需自动触发点击逻辑可加：this.handleProcessDoc(arr[0]);
      }
    },
    // 动态字典按钮点击事件
    handleProcessDoc(item) {
      this.queryParams.fileType = item.value;
      // 这里写你点击按钮后的逻辑，比如弹窗、跳转等
      // this.$message.info('你点击了: ' + item.label);
      // console.log(item)
    }
  }
};
</script>